package cn.edu.zafu.bigdata.x.wgj.k;

import java.sql.SQLException;
import java.util.Scanner;

public class StudentManagementSystem {
    private static DatabaseManager dbManager = new DatabaseManager();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            String choice = scanner.nextLine();

            switch (choice) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    retrieveStudent();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    deleteStudent();
                    break;
                case "5":
                    System.out.println("退出程序。");
                    try {
                        dbManager.closeConnection();
                    } catch (SQLException e) {
                        System.out.println("关闭数据库连接时发生错误：" + e.getMessage());
                    }
                    //dbManager.closeConnection();//关闭数据库连接
                    System.exit(0);
                    break;
                default:
                    System.out.println("无效的输入，请重新输入。");
                    break;
            }
        }
    }

    private static void addStudent() {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();

        try {
            Student202220030208 student202220030208 = new Student202220030208(id, name, phone);
            dbManager.addStudent(student202220030208);
        } catch (SQLException e) {
            System.out.println("数据库错误：" + e.getMessage());
        }
    }

    private static void retrieveStudent() {
        while (true) {
            System.out.print("请输入学号检索学生信息或输入QUIT退出: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }

            try {
                Student202220030208 student202220030208 = dbManager.getStudent(id);
                if (student202220030208 != null) {
                    System.out.println(student202220030208);
                } else {
                    System.out.println("未找到该学生");
                }
            } catch (SQLException e) {
                System.out.println("数据库错误：" + e.getMessage());
            }
        }
    }

    private static void updateStudent() {
        while (true) {
            System.out.print("请输入学号更新学生信息或输入QUIT退出: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }

            System.out.print("请输入新姓名: ");
            String name = scanner.nextLine();
            System.out.print("请输入新电话: ");
            String phone = scanner.nextLine();

            try {
                Student202220030208 student202220030208 = dbManager.getStudent(id);
                if (student202220030208 != null) {
                    student202220030208 = new Student202220030208(id, name, phone);
                    dbManager.updateStudent(student202220030208);
                } else {
                    System.out.println("未找到该学生");
                }
            } catch (SQLException e) {
                System.out.println("数据库错误：" + e.getMessage());
            }
        }
    }

    private static void deleteStudent() {
        while (true) {
            System.out.print("请输入学号删除学生信息或输入QUIT退出: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }

            try {
                dbManager.deleteStudent(id);
            } catch (SQLException e) {
                System.out.println("数据库错误：" + e.getMessage());
            }
        }
    }
}